/**
 * Created by chenyong on 2015/11/9.
 */
var express = require('express');
var request = require('request');
var router = express.Router();
var config = require('./../config');
var bigpixelsrv = require('./bigpixelsrv');
var async = require("async");
//更新为redis数据缓存方式{2015-12-23 created by wangkui}
var CachemanRedis = require('cacheman-redis');
var cache = new CachemanRedis(config.cachestore);

/*返回全景图列表*/
exports.getPanos = function(req,res)
{
    var key='AllPanos';
    cache.get(key,function(err,value){
        if(err|| value==undefined||value==''){
            try
            {
                var url = "/api/panos/";
                bigpixelsrv.GetData(url,function(err,body){
                    async.forEachSeries(body.data, function(item, callback) {
                        if(item["link"]!=null){
                            item["url"]=item["link"];
                        }else{
                            item["url"]="/panos/"+item["panoid"];
                        }
                        callback();
                    },function(results){
                        cache.set(key,body,15*90*1000,function(err){
                            if(err){console.log(err);}
                        });
                        bigpixelsrv.respondData(res,err,body);
                    });
                })
            }
            catch(e)
            {
                bigpixelsrv.handleGeneralError(res,e);
            }
        } else {
            bigpixelsrv.respondData(res,err,value);
        }
    });
}

/*读取单个全景图的信息*/
exports.getPanoInfo = function(req,res)
{
    var key='panoid_'+req.params.id;
    cache.get(key,function(err,value){
        if(err|| value==undefined||value==''){
            try{
                var url = "/api/panos/" + req.params.id;
                bigpixelsrv.GetData(url,function(err,data){
                    cache.set(key,data,15*90*1000,function(err){
                        if(err){console.log(err);}
                    });
                    bigpixelsrv.respondData(res,err,data);
                });
            }catch(e){
                bigpixelsrv.handleGeneralError(res,e);
            }
        }else {
            bigpixelsrv.respondData(res,err,value);
        }
    });
};



